Skip to content

Expose exact LLM token usage for agentic queries - #2580

Open
mahikaw wants to merge 2 commits into
mainfrom
dev/mahikaw/inner-react-token-usage
Open

Expose exact LLM token usage for agentic queries#2580
mahikaw wants to merge 2 commits into
mainfrom
dev/mahikaw/inner-react-token-usage

Conversation

@mahikaw

@mahikaw mahikaw commented Aug 25, 2026

Copy link
Copy Markdown
Collaborator

Description

  • collect query-scoped provider token usage from the ReAct and selection stages
  • expose normalized input, output, and total usage through Python and service APIs
  • add retriever query --agentic --include-usage
  • preserve existing agentic CLI and Python return formats by default

Why

Consumers need exact inner-agent token accounting. Parsing logs or estimating from text does not provide reliable provider-reported usage.

Implementation

  • adds normalized usage totals with the original per-stage breakdown
  • exposes operator-owned usage through atomic pop_query_usage() calls
  • maps internal positional graph query IDs back to caller query IDs
  • collects usage in finally blocks so backend state is cleared safely
  • adds typed agentic workflow and service response models
  • returns usage: null when the provider does not report usage

Checklist

  • I am familiar with the Contributing Guidelines.
  • New or existing tests cover these changes.
  • The documentation is up to date with these changes.

…vider usage through Python, CLI, service, and MCP paths so callers can report inner ReAct tokens without approximation.
@mahikaw
mahikaw requested review from a team as code owners August 25, 2026 00:54
@mahikaw
mahikaw requested a review from edknv August 25, 2026 00:54
@greptile-apps

greptile-apps Bot commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR collects provider-reported token usage from the ReAct and selection stages and exposes normalized totals through Python, CLI, and service responses.

  • Adds query-scoped usage collection and normalization with per-stage breakdowns.
  • Adds an opt-in agentic CLI usage envelope and an additive service response field.
  • Adds lightweight ATIF trajectory generation and best-effort persistence.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
nemo_retriever/src/nemo_retriever/_agentic/nemo_agent/llm/usage.py Adds normalized input, output, and total token accounting while preserving provider stage data.
nemo_retriever/src/nemo_retriever/query/agentic.py Collects query-scoped usage from both agent operators and maps graph-position identifiers back to caller query identifiers.
nemo_retriever/src/nemo_retriever/query/workflow.py Adds a metadata-returning agentic workflow while preserving the existing hits-only function.
nemo_retriever/src/nemo_retriever/service/query_schema.py Defines additive typed usage fields for agentic service responses.
nemo_retriever/src/nemo_retriever/_agentic/nemo_agent/atif.py Builds bounded ATIF trajectories and persists them through a best-effort atomic file replacement.

Sequence Diagram

sequenceDiagram
    participant Caller
    participant Workflow
    participant ReAct
    participant Selection
    participant Provider
    Caller->>Workflow: Agentic query
    Workflow->>ReAct: Run query
    ReAct->>Provider: LLM calls
    Provider-->>ReAct: Results and token usage
    Workflow->>Selection: Rank fused candidates
    Selection->>Provider: Selection call
    Provider-->>Selection: Result and token usage
    Workflow->>ReAct: pop_query_usage(query_id)
    Workflow->>Selection: pop_query_usage(query_id)
    Workflow-->>Caller: Hits and normalized usage
Loading

Reviews (2): Last reviewed commit: "Add ATIF tracing and token usage reporti..." | Re-trigger Greptile

Comment on lines +30 to +33
@dataclass(frozen=True)
class AgenticQueryDocumentsResult:
hits: list[dict[str, Any]]
usage: dict[str, Any]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Incomplete public usage contracts

The new public result classes and usage-returning methods omit field, parameter, return, and error documentation, leaving API consumers without a defined usage shape or ownership contract. The same incomplete pattern also appears on AgenticRetrieveResult, retrieve_with_usage(), and both operator pop_query_usage() methods.

Rule Used: Every public class and function in nemo_retriever ... (source)

Prompt To Fix With AI
This is a comment left during a code review.
Path: nemo_retriever/src/nemo_retriever/query/workflow.py
Line: 30-33

Comment:
**Incomplete public usage contracts**

The new public result classes and usage-returning methods omit field, parameter, return, and error documentation, leaving API consumers without a defined usage shape or ownership contract. The same incomplete pattern also appears on `AgenticRetrieveResult`, `retrieve_with_usage()`, and both operator `pop_query_usage()` methods.

**Rule Used:** Every public class and function in nemo_retriever ... ([source](https://github.com/nvidia/nemo-retriever/blob/a48f2b12cccbcdec8ad6465b03821d6f4cc9fc80/nemo_retriever/.greptile/config.json))

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant